package com.irdstudio.efp.esb.service.client.impl;


import com.irdstudio.basic.framework.core.util.SpringContextUtils;
import com.irdstudio.basic.sequence.service.DefaultPatternDescriptor;
import com.irdstudio.basic.sequence.service.PatternDescriptor;
import com.irdstudio.basic.sequence.service.facade.PatternedLimitableSeqService;
import com.irdstudio.efp.esb.common.client.req.EsbReqAppHeadBean;
import com.irdstudio.efp.esb.common.client.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.client.req.EsbReqSysHeadBean;
import com.irdstudio.efp.esb.common.constant.ESBClientConstance;
import com.irdstudio.efp.esb.service.client.EsbBaseBean;
import com.irdstudio.efp.esb.service.client.REsbBeanCreator;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.*;


/**
 * 需求编号:2019D08--
 * 问题编号:
 * 开发人员: caoxin
 * 创建日期:2019/9/27
 * 功能描述:ESB客户端报文创建器，模仿默认构造器实现
 * 修改日期:2019/9/27
 * 修改描述:
 */
public class ReferenceESBBeanCreator implements REsbBeanCreator {

	private static final REsbBeanCreator INSTANCE = new ReferenceESBBeanCreator();
	private static final PatternDescriptor pattern =
			DefaultPatternDescriptor.of(ESBClientConstance.CnsmrSysNo +
					YEAR_2 + MONTH + DAY + HOUR + MIN + SECOND + SEQ, 6);

	/**
	 * 创建EsbReqServiceBean用于发送ESB请求
	 * @param serverCode 服务方代码
	 * @param scene 应用场景
	 * @param esbBaseBean ESB要求的参数
	 * @param body 请求体
	 * @return
	 * @throws Exception
	 */
	@Override
	public EsbReqServiceBean create(String serverCode, String scene, EsbBaseBean esbBaseBean, Object body) throws Exception {
		EsbReqSysHeadBean sysHead = buildSysHead(serverCode, scene,esbBaseBean);
		EsbReqAppHeadBean appHead = buildAppHead(esbBaseBean);
		EsbReqServiceBean reqBean = new EsbReqServiceBean();
		reqBean.setAppHead(appHead);
		reqBean.setSysHead(sysHead);
		reqBean.setBODY(body);
		return reqBean;
	}

	public static REsbBeanCreator getINSTANCE() {
		return INSTANCE;
	}

	private static DateTimeFormatter dateForm = DateTimeFormatter.ofPattern("yyyyMMdd");
	private static DateTimeFormatter timeForm = DateTimeFormatter.ofPattern("HHmmss");
	
	protected EsbReqSysHeadBean buildSysHead(String serverCode, String scene, EsbBaseBean esbBaseBean) throws Exception {
		EsbReqSysHeadBean syshead = new EsbReqSysHeadBean();
		// 要调用的服务代码
		syshead.setSvcCd(serverCode);
		// 服务的应用场景
		syshead.setScnCd(scene);
		// 网贷的系统id
		syshead.setCnsmrSysID(ESBClientConstance.CnsmrSysNo);
		// 网贷的系统id
		syshead.setOrgnlCnsmrSysID(ESBClientConstance.CnsmrSysNo);
		// 生成流水号
		String tradeSerialNo = generateESBTradeSerialNo();
		// 全局流水号
		syshead.setGlblSrlNo(StringUtils.isEmpty(esbBaseBean.getGLBLSRLNO()) ?  tradeSerialNo : esbBaseBean.getGLBLSRLNO());
		// 消费放流水号
		syshead.setCnsmrSrlNo(tradeSerialNo);
		// 当前时间
		LocalDateTime date = LocalDateTime.now();
		syshead.setTxnDt(dateForm.format(date));
		syshead.setTxnTm(timeForm.format(date));
		syshead.setTxnMd(ESBClientConstance.TxnMd.ONLINE.name());
		syshead.setUsrLng(ESBClientConstance.UsrLng.CHINESE.name());
		syshead.setFileFlg(ESBClientConstance.FileFlg.NONE.VALUE);
		syshead.setEryptMd(ESBClientConstance.EryptMd.NONE.VALUE);
		// 平台ID
		syshead.setPlafmID(StringUtils.isEmpty(esbBaseBean.getPLATFMID()) ? ESBClientConstance.PlafmID : esbBaseBean.getPLATFMID());
		// 平台用户ID
		syshead.setPlafmUsrID(StringUtils.isEmpty(esbBaseBean.getPLAFMUSRID()) ? StringUtils.EMPTY : esbBaseBean.getPLAFMUSRID());
		syshead.setCompany(ESBClientConstance.Company);


		syshead.setOrgnlTmlIdNo(StringUtils.EMPTY);
		syshead.setTmlIdNo(StringUtils.EMPTY);
		syshead.setOrgnlCnsmrSvcNo(StringUtils.EMPTY);
		syshead.setCnsmrSvcNo(StringUtils.EMPTY);
		syshead.setMACVal(StringUtils.EMPTY);
		syshead.setMACFctr(StringUtils.EMPTY);
		syshead.setPINSd(StringUtils.EMPTY);
		syshead.setSvcVerNo(StringUtils.EMPTY);
		syshead.setVerfFlg(StringUtils.EMPTY);
		syshead.setMsgTp(StringUtils.EMPTY);
		syshead.setPltfmClntNo(StringUtils.EMPTY);
		syshead.setPrdctID(StringUtils.EMPTY);
		syshead.setRelGlblSrlNo(StringUtils.EMPTY);
		return syshead;
	}


	protected EsbReqAppHeadBean buildAppHead(EsbBaseBean esbBaseBean) {
		EsbReqAppHeadBean appHead = new EsbReqAppHeadBean();
		appHead.setInstId(esbBaseBean.getINSTID());
		appHead.setUsrNo(StringUtils.isEmpty(esbBaseBean.getUSRNO()) ? ESBClientConstance.UsrNo : esbBaseBean.getUSRNO());
		appHead.setCnlTp(ESBClientConstance.CnlTp);
		
		appHead.setUsrPswd(StringUtils.EMPTY);
		appHead.setUsrLvl(StringUtils.EMPTY);
		appHead.setUsrTp(StringUtils.EMPTY);
		appHead.setTlrSrlNo(StringUtils.EMPTY);
		appHead.setUsrTp(StringUtils.EMPTY);
		appHead.setCnlDtlTp(StringUtils.EMPTY);
		appHead.setCnsmrTxnCd(StringUtils.EMPTY);
		return appHead;
	}


	/* 生成ESB系统的交易流水号(CnsmrSrlNo) */
	private String generateESBTradeSerialNo() throws Exception {
		PatternedLimitableSeqService sequenceService = SpringContextUtils.getBean(PatternedLimitableSeqService.class);
		return sequenceService.getPatternedSequence("ESB", pattern);
	}


}
